<?php

if (!defined('PUBLIC_HTML'))
{
	exit;
}

/*if (!$user->is_admin)
{
	redirect('cms.php?p=pages');
	exit;
}*/

/***********************
* TEMPLATE SETTINGS
***********************/
for ($i=0;$i<3;$i++)
{
	$tpl->zone('textblock', array(
		'I' => $i,
	));
}

/***********************
* ADDING THE PAGE
************************/
$submit	= (isset($_POST['submit'])) ? true : false;

if ($submit)
{
	$post = array();
	$post['page_mod_id'] = 0;
	$post['page_parent_id'] = (int) $_POST['parent_id'];
	$post['page_title']	= $_POST['page_title'];
	$post['page_full'] = 1;
	$post['page_type'] = 1;
	
	$database->sqlInsert(TABLE_PAGES, $post);
	
	$get_added_page = 	"
						SELECT		page_id
						FROM		" . TABLE_PAGES . "
						ORDER BY	page_id
						DESC
						LIMIT		0,1
						";
	$database->query($get_added_page);
	$row_added_page = $database->single();
	
	unset($post);
	$post = array();
	$post['pct_page_id'] 	= $row_added_page['page_id'];
	$post['pct_lang']		= 'en';
	$post['pct_var']		= $string->clean($_POST['page_title']);
	$post['pct_type']		= 'default';
	$post['pct_title']		= (string) $_POST['page_title'];
	$post['pct_content']	= (string) $db->sql_injection($_POST['content']);
	$post['pct_module']		= '';
	$post['pct_module_part']= '';
	$post['pct_row']		= '1';
	$post['pct_rowspan']	= '1';
	$post['pct_col']		= '1';
	$post['pct_colspan']	= '1';
	$post['pct_position']	= '1';
	$database->sqlInsert(TABLE_PAGES_CONTENT, $post);
	
	if ($_POST['add_menu'] == 1)
	{
		$post = array();
		$post['page_id']	= $row_added_page['page_id'];
		$post['link']		= $added_page_var;
		$post['target']		= (isset($_POST['menu_target']))		? ((!empty($_POST['menu_target']))	? (string) $string->secure($_POST['menu_target'],	'sql_single_field') : '_self') 		: '_self';
		$post['title']		= (isset($_POST['menu_title']))		? ((!empty($_POST['menu_title']))	? (string) $string->secure($_POST['menu_title'],		'sql_single_field') : 'undefined') 	: 'undefined';
		$post['lang']		= 'en';
		$database->sqlInsert(TABLE_MENU, $post, 'men_');
	}
	
	redirect('cms.php?p=pages');
	exit;
}

// Get Pages for the Subpage dropdown
$get_pages = 	"
				SELECT		*
				FROM		" . TABLE_PAGES . "
				WHERE		page_parent_id = :page_parent_id
				";
$database->query($get_pages);
$database->bind(':page_parent_id', 0);
foreach ($database->resultSet() as $row_pages)
{
	$tpl->zone('pages', array(
		'ID'		=> (int)	$row_pages['page_id'],
		'TITLE'		=> (string)	$row_pages['page_title'],
	));
}


?>